﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Linq;
using CoolCode.WebServices;

namespace CoolCode.Web.UI.Views.Inquiry
{
    public partial class Products : SearchPage<Inspection, IInspectionService>  //Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            handleEvents();
            if (!IsPostBack)
            {
            }
        }

        public override IQueryBuilder<Inspection> CreateFilter()
        {
            return productFilter.CreateFilter();
        }

        protected void handleEvents()
        {
            this.Setup(lnkSearch, productList, c =>
            {
                productList.Model = c.Result ;
                productList.DataBind();
                udpHeader.Update();
                //summary
                var qb = productFilter.CreateFilter();
                var qbGroup = QueryBuilder.Create<Inspection>();
                var summary = Service.Summary(qb, qbGroup);
                productSummary.Model = summary;
                productSummary.DataBind();
                udpSummary.Update();
            },
            c =>
            {
                udpHeader.Alert(c.Exception);
            });

            //pgrMaster.PageIndexChanging += (sender, e) => this.ProvideData(new PageParam(e.NewPageIndex, this.PageSize));             

            this.Setup(exporter, new ExportService());

        }

        //private IInspectionService _Service;
        //public IInspectionService Service
        //{
        //    get
        //    {
        //        if (_Service == null)
        //        {
        //            _Service = ServiceFactory.Produce<IInspectionService>();
        //        }
        //        return _Service;
        //    }
        //}

        //private const int PageSize = 10;

        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    handleEvents();

        //    if (!IsPostBack)
        //    {
        //        //cdrCheckDateFr.Value = DateTime.Now.Date.AddMonths(-12);
        //        //cdrCheckDateTo.Value = DateTime.Now.Date;
        //        //productList.Load += (s, ev) => pgrMaster_PageIndexChanging(s, new GridViewPageEventArgs(0)); //BindData_Master(new PageParam(0, PageSize));// new EventHandler(productList_Load);

        //        //ddlStatus.Bind(Service.ListStatus(true));
        //    }
        //}

        ////protected  void productList_Load(object sender, EventArgs e)
        ////{
        ////    BindData_Master(new PageParam(0, PageSize));
        ////}

        //protected void handleEvents()
        //{
        //    //GridViewHelper.HighlightStatus(gvMaster);
        //    //GridViewHelper.HighlightCriterion(gvMaster);
        //    //pgrMaster.PageIndexChanging += new GridViewPageEventHandler(pgrMaster_PageIndexChanging); 
        //    productList.PageIndexChanging += new GridViewPageEventHandler(pgrMaster_PageIndexChanging);
        //    lnkSearch.Click += new EventHandler(lnkSearch_Click);
        //    exporter.Setup(new ExportService(), () =>
        //        {
        //            var qb = productFilter.CreateFilter();
        //            var list = Service.List(qb);
        //            return list;
        //        });

        //    // lnkExcel.AsExportable(new ExportService(), () =>
        //    //     {
        //    //         var qb = productFilter.CreateFilter();
        //    //         var list = Service.ListInspection(qb);
        //    //         return list;
        //    //     },
        //    //    c => { ViewHelper.BindFilePath(lnkExcelSrc, c.FilePath); udpExcel.Update();},
        //    //    c => udpHeader.Alert(c.Message)
        //    //);
        //}

        //protected void lnkSearch_Click(object sender, EventArgs e)
        //{
        //    BindData_Master(new PageParam(0, PageSize));
        //    // gvMaster.Visible = true; 
        //    udpHeader.Update();
        //    udpSummary.Update();
        //}

        //private void BindData_Master(PageParam p)
        //{
        //    //var qb = QueryBuilder.Create<Inspection>()
        //    //     .Fuzzy(c => c.ProductID, txtProductID.Text)
        //    //     .Fuzzy(c => c.ProductSpec, txtProductSpec.Text)
        //    //     .Between(c => c.CheckDate, cdrCheckDateFr.Value.GetValueOrDefault(), cdrCheckDateTo.Value.GetValueOrDefault())
        //    //     .Equals(c => c.Status, ddlStatus.SelectedValue, ProductStatus.All.GetValue());
        //    var qb = productFilter.CreateFilter();
        //    var list = Service.Paging(qb, p);
        //    productList.Model = list;
        //    productList.DataBind();

        //    //summary
        //    var qbGroup = QueryBuilder.Create<Inspection>();
        //    var summary = Service.Summary(qb, qbGroup);
        //    productSummary.Model = summary;
        //    productSummary.DataBind();
        //}

        //protected void pgrMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    BindData_Master(new PageParam(e.NewPageIndex, PageSize));
        //}



    }
}
